﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Chapter21.View
{
    using Chapter21.ViewModel;
    using System.Data;
    using System.Collections;
    using System.Collections.ObjectModel;
    using System.ComponentModel;

    /// <summary>
    /// BindProductObject.xaml 的交互逻辑
    /// </summary>
    public partial class ItemsGroup3Window : Window
    {
        private DataTable products;
        private BindingListCollectionView view;

        public ItemsGroup3Window()
        {
            InitializeComponent();
        }

        private void getDataButton_Click(object sender, RoutedEventArgs e)
        {
            products = App.StoreDBDataSet.GetProducts();
            productsListBox.ItemsSource = products.DefaultView;
            dateTextBox.DataContext = DateTime.Today;
            view = (BindingListCollectionView)CollectionViewSource.GetDefaultView(productsListBox.ItemsSource);
            view.SortDescriptions.Add(new SortDescription("UnitCost", ListSortDirection.Ascending));
            var grouper = new PriceRangeProductGrouper { GroupInterval = 50 };
            view.GroupDescriptions.Add(new PropertyGroupDescription("UnitCost", grouper));
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            products.DefaultView.Delete(productsListBox.SelectedIndex);
        }

        private void changedButton_Click(object sender, RoutedEventArgs e)
        {
            products.AcceptChanges();
            products.DataSet.WriteXml("store.xml");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            decimal minimunPrice;
            if (decimal.TryParse(filterTextBox.Text, out minimunPrice))
            {
                if (view != null)
                {
                    view.CustomFilter = "UnitCost >" + minimunPrice.ToString();
                }
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            view.CustomFilter = null;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            products.DefaultView.Sort = "ModelNumber";
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            products.Rows.Add(new object[] { null, 16, "ModelNumber", "ModeName", "ProductImage", 100, "Description", "16", "CategoryName" });
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {

        }
    }
}
